﻿#include "stdafx.h"
#include "LoginUserInfo.h"
#include "BaseInclude.h"

LoginUserInfo * LoginUserInfo::sysRecords = NULL;

LoginUserInfo::LoginUserInfo(QObject *parent)
	:QObject(parent)
{

}

LoginUserInfo::~LoginUserInfo()
{

}

LoginUserInfo * LoginUserInfo::getInstance()
{
	if (sysRecords)
		return sysRecords;

	sysRecords = new LoginUserInfo;
	return sysRecords;
}

bool LoginUserInfo::initLoginUserInfo(QString userid, QString username,QString usercode)
{
	strUserID = userid;
	strUserName = username;
	strUserCode = usercode;
	if ( readUserDeptInfo() == false )
	{
		clearLoginUserInfo();
		return false;
	}

	if (readUserRoleInfo() == false)
	{
		clearLoginUserInfo();
		return false;
	}

	if (readUserDeptInfo() == false)
	{
		clearLoginUserInfo();
		return false;
	}

	if (readUserStationInfo() == false)
	{
		clearLoginUserInfo();
		return false;
	}
	if (readUserLimitInfo() == false)
	{
		clearLoginUserInfo();
		return false;
	}

	if (readUserCmpyInfo() == false)
	{
		clearLoginUserInfo();
		return false;
	}
	return true;
}

void LoginUserInfo::clearLoginUserInfo()
{
	strUserName = "";
	strUserID = "";
	strUserCode = "";
	strDeptName = "";
	strDeptID = "";
	strCompanyName = "";
	strCompanyID = "";
	listRolePatternID.clear();
	listRoleID.clear();		 
	listStationID.clear();
	listUserLimit.clear();
}

bool LoginUserInfo::getUserRole(int iPatternID, QList<int> *pListRole)
{
	CHK_EXP_RET(pListRole,false);
	pListRole->clear();
	int icount = listRolePatternID.count();
	for (int i = 0; i < icount; i++)
	{
		if (listRolePatternID[i] == iPatternID)
		{
			pListRole->append(listRoleID[i]);
		}
	}

	return true;
}

bool LoginUserInfo::isHaveRole(int iPatternID, int iRoleID)
{
	int icount = listRolePatternID.count();
	for (int i = 0; i < icount; i++)
	{
		if (listRolePatternID[i]==iPatternID&&listRoleID[i]==iRoleID)
		{
			return true;
		}
	}

	return false;
}

bool LoginUserInfo::iSHaveLimit(int iLimitID)
{
	int icount = listUserLimit.count();
	for (int i = 0; i < icount;i++)
	{
		if (listUserLimit[i] == iLimitID)
		{
			return true;
		}
	}

	return false;
}

bool LoginUserInfo::canCtrStation(int iStationID)
{
	int icount = listStationID.count();
	for (int i = 0; i < icount;i++)
	{
		if (listStationID[i]==iStationID)
		{
			return true;
		}
	}

	return false;
}

//======================================================================================================================================
bool LoginUserInfo::readUserDeptInfo()
{
	QRecord dsRecord;
	QString sql = QString("SELECT b.ORGANISEID AS DEPTID, b.ORGANISENAME AS NAME, a.PASSWD AS PWD\
						  	FROM JXD7_XT_USER a, JXD7_XT_ORGANISE b WHERE a.USERID = '%1' \
							AND a.DEPTID = b.ORGANISEID").arg(strUserID);
	CHK_EXP_RET(!dsRecord.select(sql),false);
	
	if (dsRecord.rowCount() >= 1)
	{ 
		strDeptID = dsRecord.getCollectStr(0, "DEPTID");
		strDeptName = dsRecord.getCollectStr(0, "NAME");
		strPasswd = dsRecord.getCollectStr(0, "PWD");
	}

	return true;
}

bool LoginUserInfo::readUserRoleInfo()
{
	QRecord dsRecord;
	QString sql = QString("SELECT PATTERNID,ROLEID FROM WS_USERROLE WHERE USERID = '%1'").arg(strUserID);
	CHK_EXP_RET(!dsRecord.select(sql), false);

	for (int i = 0; i < dsRecord.rowCount(); i++)
	{
		listRolePatternID.append(dsRecord.getCollectInt(i, "PATTERNID"));
		listRoleID.append(dsRecord.getCollectInt(i, "ROLEID"));
	}

	return true;
}

bool LoginUserInfo::readUserStationInfo()
{
	QRecord dsRecord;
	QString sql = QString("SELECT STATIONID, MODULETYPE FROM WS_USERSTATION WHERE USERID = '%1'").arg(strUserID);
	CHK_EXP_RET(!dsRecord.select(sql), false);

	for (int i = 0; i < dsRecord.rowCount(); i++)
	{
		listStationID.append(dsRecord.getCollectInt(i, "MODULETYPE") * 1000 + dsRecord.getCollectInt(i, "STATIONID"));
	}

	return true;
}

bool LoginUserInfo::readUserLimitInfo()
{
	QRecord dsRecord;
	QString sql = QString("SELECT WS_USERLIMIT.LIMITID FROM WS_USERLIMIT,WS_LIMITINFO \
						  		WHERE WS_USERLIMIT.USERID = '%1' \
								AND WS_USERLIMIT.LIMITID = WS_LIMITINFO.LIMITID").arg(strUserID);
	CHK_EXP_RET(!dsRecord.select(sql), false);

	for (int i = 0; i < dsRecord.rowCount(); i++)
	{
		listUserLimit.append(dsRecord.getCollectInt(i,"LIMITID"));
	}

	return true;
}

bool LoginUserInfo::readUserCmpyInfo()
{
	return true;
}

QString LoginUserInfo::getUserName()
{
	return strUserName;
}

QString LoginUserInfo::getUserId()
{
	return strUserID;
}

QString LoginUserInfo::getDeptName()
{
	return strDeptName;
}

QString LoginUserInfo::getDeptId()
{
	return strDeptID;
}

QString LoginUserInfo::getPasswd()
{
	return strPasswd;
}
